//快手
const { getKuaiShouRoomInfo } = require('./kuaishou/roomInfo')
const kuaishouClient = require('./kuaishou/kuaishou')
//抖音
const { getDouYinRoomInfo } = require('./douyin/roomInfo')
const douyinClient = require('./douyin/douyin')

//解析入口类
class entryMain {
	//构造函数
	constructor(parmas) {
		//初始化快手/抖音客户端实例对象池
		if(parmas.soure == 'ks') parmas.getClientPool(parmas.soure)[parmas.socket.clientId].client = new kuaishouClient(); //快手客户端实例
		if(parmas.soure == 'dy') parmas.getClientPool(parmas.soure)[parmas.socket.clientId].client = new douyinClient(); //抖音客户端实例
		//新增参数
		this.ksUrl = 'https://live.kuaishou.com/u/';
		this.dyUrl = 'https://live.douyin.com/';
		//传入参数
		this.wsServer = parmas.socket; // 客户端socket对象
		this.roomId = parmas.roomId; // ws url中的id
		this.key = parmas.key; // ws url中的key
		this.soure = parmas.soure; // ws url中的soure - 平台标识 ks/dy
		this.buildStrJSON = parmas.buildStrJSON; // 构建json字符串的方法
		this.sendMsg = parmas.sendMsg; // 发送消息的方法
		this.closeSocket = parmas.closeSocket; // 关闭连接的方法
		this.consoleLog = parmas.consoleLog; // 控制台日志输出
		this.setGlobalData = parmas.setGlobalData; //修改全局变量数据
		this.getGlobalData = parmas.getGlobalData; //获取全局变量数据
		this.getClientPool = parmas.getClientPool; //快手 抖音 获取客户端池
		//this.serverConnected = parmas.serverConnected; // 是否连接 - 客户端连接到本websocket服务标识
		//this.initEntry = parmas.initEntry // 初始化入口方法
		//快手
		if(this.soure === 'ks') this.startKuaiShou();
		//抖音
		if(this.soure === 'dy') this.startDouYin();
	}

	//快手
	async startKuaiShou(){
		/*
		//快手测试数据
		const ksKiveInfo = {
			wssAddress: 'wss://live-ws-group3.kuaishou.com/websocket',
			token: 'Kesr+dKkw/2TpV+ss76b2lL3+xt2OUw5PDkcQIbQiIw5R3rHFME9s8DJUUEzTqMr1I55FeooZVFSk1tmxBjNgz7HXNyYlwUn+nR59Hn5ufDj3k1PmuAkRcWCLc2cyFAFcVIFOxFntrm6DbOmrAcQmA==',
			liveStreamId: 'mCZViGh7kOA',
			cookie: '',
			cookieObj: {},
		}
		ksKiveInfo.entryMain = this;
		return this.getClientPool(this.wsServer.soure)[this.wsServer.clientId].client.setConfig(ksKiveInfo).start();
		*/
		const ksLiveUrl = this.ksUrl + this.roomId;
		getKuaiShouRoomInfo(ksLiveUrl).then(ksKiveInfo => {
			ksKiveInfo.entryMain = this;
			//this.consoleLog(`ksKiveInfo>>>${ksKiveInfo}`)
			this.getClientPool(this.wsServer.soure)[this.wsServer.clientId].client.setConfig(ksKiveInfo).start();
		}).catch(err => {
			this.consoleLog(`[ks-${this.roomId}]>>>${err}`)
			this.sendMsg(this.buildStrJSON(402, `[ks-${this.roomId}]>>>${err}`), this.wsServer.clientId)
		})
	}

	//抖音
	startDouYin(){
		/*
		//抖音测试数据
		const dyLiveInfo = {
			userUniqueId: '7466679192291509810',
			roomId: '7466619162513738496',
			cookie: ' ttwid=1%7Ccl8cdf4NQN8IjxLOLpA7tAxDKIDSZr32xKLGCNl94vQ%7C1738471746%7C04eb552c2608093793a719c88957ba1d7f55544721a363029840fa29932f47ae',
			cookieObj: {
			  ttwid: '1|cl8cdf4NQN8IjxLOLpA7tAxDKIDSZr32xKLGCNl94vQ|1738471746|04eb552c2608093793a719c88957ba1d7f55544721a363029840fa29932f47ae'
			}
		}
		dyLiveInfo.entryMain = this;
		this.getClientPool(this.wsServer.soure)[this.wsServer.clientId].client.setConfig(dyLiveInfo).start();
		return
		*/
		const dyLiveUrl = this.dyUrl + this.roomId;
		getDouYinRoomInfo(dyLiveUrl).then(dyLiveInfo => {
			dyLiveInfo.entryMain = this;
			//this.consoleLog(`dyLiveInfo>>>${dyLiveInfo}`)
			this.getClientPool(this.wsServer.soure)[this.wsServer.clientId].client.setConfig(dyLiveInfo).start();
		}).catch(err => {
			this.consoleLog(`[dy-${this.roomId}]>>>${err}`)
			this.sendMsg(this.buildStrJSON(402, `[dy-${this.roomId}]>>>${err}`), this.wsServer.clientId)
		})
	}
}

module.exports = entryMain